#include <std.h>
#include <memory.h>

/*This module registers configurable settings from the kernel at runtime during inits and then allows the user to
change the setting as he wishes*/

#define SDESC_SZ 64	//The variable description size of a setting

static struct setting	{
	char sdesc[SDESC_SZ];
	char *str;
	unsigned *val;
	struct setting *next;
}*head_setting=NULL;

int create_setting_val(char *desc,unsigned *val)
{
	struct setting *x,*t;
	x=new(struct setting);
	strcpy(x->sdesc,desc);
	x->val=val;
	x->str=NULL;
	x->next=NULL;
	if(head_setting==NULL)	{
		head_setting=x;
		return NULL;
	}
	for(t=head_setting;t->next;t=t->next);	//Reach the last node in the linked list
	t->next=x;
	return NULL;
}

unsigned read_setting_val(char *desc)
{
	struct setting *t;
	for(t=head_setting;t;t=t->next)	{
		if(!strcmp(t->sdesc,desc))	{
			return (t->val)? (*(t->val)):NULL;
		}
	}
}

int write_setting_val(char *desc,unsigned val)
{
	struct setting *t;
	for(t=head_setting;t;t=t->next)	{
		if(!strcmp(t->sdesc,desc))	{
			if(t->val)
				*(t->val)=val;
			return NULL;
		}
	}
}

int create_setting_str(char *desc,char *str)
{
	struct setting *x,*t;
	x=new(struct setting);
	strcpy(x->sdesc,desc);
	x->str=str;
	x->val=NULL;
	x->next=NULL;
	if(head_setting==NULL)	{
		head_setting=x;
		return NULL;
	}
	for(t=head_setting;t->next;t=t->next);	//Reach the last node in the linked list
	t->next=x;
	return NULL;
}

char *read_setting_str(char *desc)
{
	struct setting *t;
	for(t=head_setting;t;t=t->next)	{
		if(!strcmp(t->sdesc,desc))	{
			return t->str;
		}
	}
}

int write_setting_str(char *desc,char *str)
{
	struct setting *t;
	for(t=head_setting;t;t=t->next)	{
		if(!strcmp(t->sdesc,desc))	{
			strcpy(t->str,str);
			return NULL;
		}
	}
}

int list_settings()
{
	struct setting *t;
	printk("\nRegistered settings:");
	for(t=head_setting;t;t=t->next)	{
		printk("\n%s:%d,%s",t->sdesc,*(t->val),(t->str)? t->str:"");
	}
}

int set_init()
{
}

